package com.gegong.imr.client.bean;


import com.gegong.imr.client.vbox.cmd.*;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.virtualbox_5_2.VirtualBoxManager;

import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

@Slf4j
public enum TaskCmd {

    copy_virtual("1", "复制虚拟机", "copy_virtual", "", CopyVMCmd.class),
    modify_virtual("2", "复制虚拟机,修改虚拟机名称和mac,修改虚拟机名称和mac", "modify_virtual", "", CopyVMCmd.class),
    start_virtual("3", "启动虚拟机", "start_virtual", "", VMPowerCmd.class),
    shutdown_virtual("4", "关机虚拟机", "shutdown_virtual", "", VMPowerCmd.class),
    restart_virtual("5", "重启虚拟机", "restart_virtual", "", VMPowerCmd.class),
    del_virtual("6", "删除虚拟机", "del_virtual", "", VmManageCmd.class),
    replace_wuma("7", "更换五码", "replace_wuma", "", null),
    send_single_msg("8", "发送单条短信", "send_single_msg", "", null),
    send_batch_msg("9", "发送批量短信", "send_batch_msg", "", null),
    other("99", "其他", "other", "", null);

    private String code;
    private String display;
    private String name;
    private String value;
    private Class<? extends IVBoxCmd> cmdClass;

    private TaskCmd(String code, String display, String name, String value, Class<? extends IVBoxCmd> cmdClass) {
        this.code = code;
        this.display = display;
        this.name = name;
        this.value = value;
        this.cmdClass = cmdClass;
    }


    public String getCode() {
        return code;
    }

    public String getDisplay() {
        return display;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public Class<? extends IVBoxCmd> getCmdClass() {
        return cmdClass;
    }

    //获取枚举实例
    public static TaskCmd fromValue(String code) {
        for (TaskCmd statusEnum : TaskCmd.values()) {
            if (Objects.equals(code, statusEnum.getCode())) {
                return statusEnum;
            }
        }
        return TaskCmd.other;
    }

    public IVBoxCmd build(@NonNull VirtualBoxManager virtualBoxManager, @NonNull String targetVm) throws CreateVMCmdFailException {
        try {
            return this.cmdClass.getConstructor(VirtualBoxManager.class, String.class).newInstance(virtualBoxManager, targetVm);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new CreateVMCmdFailException(e, targetVm, this.cmdClass);
        }
    }
}
